package com.sky;

import com.sky.entity.Category;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;

public class RedisType {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void test(){
//        //redis就是一个超大map集合 key (string)、value(支持5钟)
//        HashMap<String, Object> redis = new HashMap<>();
//
//        //1. 字符串类型
//        redis.put("jack","i am a string");
//        //2.哈希类型
//        HashMap<String, Object> hash = new HashMap<>();
//        hash.put("name","jack");
//        hash.put("age",18);
//
//        redis.put("myhash",hash);
//
//        //3. 列表类型
//        LinkedList<Object> list = new LinkedList<>();
//        list.add("a");
//        list.add("b");
//        list.add("a");
//
//        redis.put("mylist",list);
//
//        //4.集合类型
//        HashSet<Object> set = new HashSet<>();
//        set.add("a");
//        set.add("b");
//        set.add("c");
//
//        redis.put("myset",set);
//
//        //5.有序集合
//        SortedSet<Object> zset = new TreeSet<>();
//        zset.add("a");
//        zset.add("b");
//        zset.add("c");
//
//        redis.put("myzset",zset);

        //1. 字符串类型
        ValueOperations valueOperations = redisTemplate.opsForValue();
        //2. 哈希类型
        HashOperations hashOperations = redisTemplate.opsForHash();
        //3. 列表类型
        ListOperations listOperations = redisTemplate.opsForList();
        //4.集合类型
        SetOperations setOperations = redisTemplate.opsForSet();
        //5.有序集合类型
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
    }

    public void test02(){
        //1. 字符串操作对象
        ValueOperations valueOperations = redisTemplate.opsForValue();

        //  存
        valueOperations.set("mystr","i am a string");
        // 取
        System.out.println(valueOperations.get("mystr"));

        //存短信验证码，5分钟有效时间
        valueOperations.set("sms:1350456465645","223355", Duration.ofSeconds(300));

        //存一个java对象
        Category category = Category.builder()
                .name("烤鱼套餐")
                .status(1)
                .createTime(LocalDateTime.now())
                .build();
        valueOperations.set("category",category);
        //查询java对象
        System.out.println(valueOperations.get("category"));
    }

    @Test
    public void test03(){
        //2. 哈希操作对象
        HashOperations hashOperations = redisTemplate.opsForHash();
        //存
        hashOperations.put("user:1","name","饭岛爱");
        hashOperations.put("user:1","age",18);

        //取一个
        System.out.println("---------------------");

        //取出所有元素
        Set hkeys = hashOperations.keys("user:1");
        hkeys.forEach(hkey->{
            System.out.println(hashOperations.get("user:1",hkey));
        });
        //删除
        hashOperations.delete("user:1","age");
    }

    //列表类型
    @Test
    public void test04(){
        //3.列表操作对象
        ListOperations listOperations = redisTemplate.opsForList();
        //左压入
        listOperations.leftPushAll("mylist","a","b","c");

        //查询 c b a
        List mylist = listOperations.range("mylist", 0, -1);
        System.out.println(mylist);

        //右弹出
        System.out.println(listOperations.rightPop("mylist"));

        //查询 c b
        List mylist1 = listOperations.range("mylist", 0, -1);
        System.out.println(mylist1);
    }

    //集合类型
    @Test
    public void test5(){
        //4.集合操作对象
        SetOperations setOperations = redisTemplate.opsForSet();

        // 存 蔡徐坤
        setOperations.add("caixukun","唱","跳","rap","打篮球");
        // 存郭奥哲
        setOperations.add("guoanzhe","爱坤","唱","rap");

        //查询
        Set set = setOperations.members("guoaozhe");
        System.out.println(set);

        //交集
        System.out.println(setOperations.intersect("caixukun","guoaozhe"));
        //并集
        System.out.println(setOperations.union("caixukun","guoaozhe"));

        //删除
        setOperations.remove("caixukun","rap");
    }

    //有序集合类型
    @Test
    public void test06(){
        //5. 有序集合操作对象
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();

        //存
        zSetOperations.add("ys","后羿",66.4);
        zSetOperations.add("ys","安其拉",45.2);
        zSetOperations.add("ys","妲己",23.2);

        // 苦练安其拉
        zSetOperations.incrementScore("ys","安其拉",30);
        //查询 降序
        System.out.println(zSetOperations.reverseRange("ys",0,-1));
    }

    //通用操作
    @Test
    public void test07(){
        // 查询 keys c*
        Set keys = redisTemplate.keys("c*");
        System.out.println(keys);

        //批量删除
        redisTemplate.delete(keys);
        //判断key是否存在
        System.out.println(redisTemplate.hasKey("caixukun"));
    }








}
